﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    class DProjects
    {
        private static string query;

        /*
         * The following function shows the log of the employee
         */
        public static DataTable showMyLog(string id)
        {
            DataTable dt = new DataTable();
            query = "SELECT * FROM tbl_Logs WHERE fld_employeeID Like '" + id + "'";
            try
            {
                dt = AdoHelper.ExecuteDataTable(query);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return dt;
        }

        /*
         * The following function shows all of the projects
         * of the logged in employee.
         */
        public static DataTable showMyProjects(string id)
        {
            DataTable dt = new DataTable();
            query = "SELECT * FROM tbl_ProjectsOfEmployees WHERE fld_employeeID Like '" + id + "'";
            dt = AdoHelper.ExecuteDataTable(query);
            DataRow[] dr;
            StringBuilder eQuery = new StringBuilder();
            string start = "SELECT * FROM tbl_Projects WHERE";
            eQuery.Append(start);
            dr = dt.Select();
            for (int i = 0; i < dr.Length; i++)
            {
                eQuery.Append(" AND fld_projectID=" + dr[0].Field<int>("fld_projectID"));
            }
            try
            {
                eQuery.Remove(start.Length + 1, 4);
                dt = AdoHelper.ExecuteDataTable(eQuery.ToString());
            }
            catch
            {
                MessageBox.Show("No employees for this project", "Error");
            }
            return dt;
        }

        public static void check(int logID)
        {
            query = "UPDATE tbl_Logs SET fld_isDone=TRUE WHERE fld_logID=" + logID;
            AdoHelper.ExecuteNonQuery(query);
        }

        public static void unCheck(int logID)
        {
            query = "UPDATE tbl_Logs SET fld_isDone=FALSE WHERE fld_logID=" + logID;
            AdoHelper.ExecuteNonQuery(query);
        }
    }
}
